package com.cy.shiro.controller;

import com.alibaba.fastjson.JSONObject;
import com.cy.shiro.consts.CommonConst;
import com.cy.shiro.pojo.SysResources;
import com.cy.shiro.service.SysResourcesService;
import com.cy.shiro.service.SysUserService;
import com.cy.shiro.util.ResultObject;
import com.cy.shiro.util.ResultUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class LoginController {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysResourcesService sysResourcesService;
    @Autowired
    private KaptchaController kaptchaController;
    @Autowired
    private BaseController baseController;


    //后台首页
    @RequestMapping("index")
    public String re(Model model){

        baseController.sysResources("后台首页", "/index", model);

        return  "/index";
    }



    @RequiresPermissions("users:add")
    @RequestMapping("/user")
    public String userTag(Model model){

        List<SysResources> sysResources = sysResourcesService.resourcesList(1);
        System.out.println(sysResources);

        model.addAttribute("sysResources", sysResources);

        model.addAttribute("title", "标签列表");
        model.addAttribute("active", "/user");
        return "user/tags";
    }

    //登录页面
    @RequestMapping("/login")
    public String login(){

        return "login";
    }

    /**
     * 登录验证
     * @return
     */
    @CrossOrigin
    @PostMapping("/postLogin")
    @ResponseBody
    public Object postLogin(HttpServletRequest request){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String kaptcha  = request.getParameter("kaptcha");

//        ResultObject resultObject = kaptchaController.checkVerificationCode(kaptcha, request);
//        if(resultObject.getStatus() == 500){
//            return resultObject;
//        }

        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);

        Integer code = CommonConst.DEFAULT_ERROR_CODE;
        String message = "";
        String redirect = "";

        try{
            System.out.println("开始登录认证："+ username);
            subject.login(token);
            System.out.println("登录认证成功："+ username);
            code = CommonConst.DEFAULT_SUCCESS_CODE;
            message = "登录成功";
            redirect = "/index";
        }catch (UnknownAccountException e){
            message = "用户名不存在或被禁用";
        }catch (IncorrectCredentialsException e){
            message = "密码错误";
        }

        //清除缓存
        if(code != 200){
            token.clear();
        }

        List<SysResources> sysResources = sysResourcesService.resourcesList(1);
        Map<String, Object> params = new HashMap<>();
        params.put("redirect", redirect);
        params.put("sysResources", sysResources);

        return ResultUtil.json(code, message, params);
    }

    @RequiresPermissions("users:add")
    @RequestMapping("/users/add")
    public String add(){
        return "/user/add";
    }

    @RequiresPermissions("users:edit")
    @RequestMapping("/users/edit")
    public String edit(){
        return "/user/edit";
    }




    @RequestMapping("/list")
    public String list(){
        return  "list";
    }

    @RequestMapping("/noAuth")
    public String noAuth(){
        return "error";
    }

    @RequestMapping("/logout")
    public String logout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();

        return "redirect:login";
    }
}
